﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogic.BusinessEntity.Entities;
using UserInterface.ClientApplication.UIEntity;


namespace UserInterface.ClientApplication
{
	public partial class StatementGrid : Form, IForm
	{
		public StatementGrid()
		{
			InitializeComponent();
		}

		private void StatementGrid_Load(object sender, EventArgs e)
		{
			var param = (Object[])fm.Param;
			var DateFrom = (DateTime)param[0];
			DateFrom = new DateTime(DateFrom.Year,DateFrom.Month,DateFrom.Day,0,0,0);
			var DateTo = (DateTime)param[1];
			var account = (AccountView)param[2];
			DateTo = new DateTime(DateTo.Year, DateTo.Month, DateTo.Day, 23, 59, 59);
			this.Text += " c " + DateFrom.ToString("dd MMMM yyyy") + " по " + DateTo.ToString("dd MMMM yyyy");
			StatementDataGridView.AutoGenerateColumns = false;

			DataGridViewTextBoxColumn PayerNumberColumn = new DataGridViewTextBoxColumn();

			PayerNumberColumn.DataPropertyName = "PayerPartyAccount";
			PayerNumberColumn.HeaderText = "Номер счета отправителя";
DataGridViewTextBoxColumn RecepientNumberColumn = new DataGridViewTextBoxColumn();
			RecepientNumberColumn.DataPropertyName = "BillToPartyAccount";
			RecepientNumberColumn.HeaderText = "Номер счета получателя";
			DataGridViewTextBoxColumn BalanceColumn = new DataGridViewTextBoxColumn();
			BalanceColumn.DataPropertyName = "TotalAmount";
			BalanceColumn.HeaderText = "Сумма";
			DataGridViewTextBoxColumn CurrencyColumn = new DataGridViewTextBoxColumn();
			CurrencyColumn.DataPropertyName = "Currency";
			CurrencyColumn.HeaderText = "Валюта";

			DataGridViewTextBoxColumn DateColumn = new DataGridViewTextBoxColumn();
			DateColumn.DataPropertyName = "IssueDate";
			DateColumn.HeaderText = "Дата оплаты";

			StatementDataGridView.Columns.Add(PayerNumberColumn);
			StatementDataGridView.Columns.Add(RecepientNumberColumn);
			StatementDataGridView.Columns.Add(BalanceColumn);
			StatementDataGridView.Columns.Add(CurrencyColumn);
			StatementDataGridView.Columns.Add(DateColumn);
			BindingList<RemittanceAdviceView> Statements = null;
			List<CurrencyConversion>  currencyConversion = null;
			new ExceptionCatcher().Execute(() =>
			{ Statements =  fm.GetStatement(DateFrom, DateTo, account);
				});
			StatementDataGridView.DataSource = Statements;
			decimal sum = 0;
			for(int i = 0; i < Statements.Count; i++)
			{
				if (fm.GetMyAccountsByNumber(Statements[i].BillToPartyAccount.Number) != -1)
				{
					StatementDataGridView.Rows[i].Cells[0].Style.BackColor = Color.LightGreen;
					StatementDataGridView.Rows[i].Cells[1].Style.BackColor = Color.LightGreen;
					StatementDataGridView.Rows[i].Cells[2].Style.BackColor = Color.LightGreen;
					StatementDataGridView.Rows[i].Cells[3].Style.BackColor = Color.LightGreen;
					StatementDataGridView.Rows[i].Cells[4].Style.BackColor = Color.LightGreen;
					if (account.Currency.Name != Statements[i].Currency.Name)
					{
						sum += Statements[i].TotalAmount / fm.GetRate(account.Currency, Statements[i].Currency);
					}
					else
					{
						sum += Statements[i].TotalAmount;
					}
				}
				else
				{
					StatementDataGridView.Rows[i].Cells[0].Style.BackColor = Color.LightPink;
					StatementDataGridView.Rows[i].Cells[1].Style.BackColor = Color.LightPink;
					StatementDataGridView.Rows[i].Cells[2].Style.BackColor = Color.LightPink;
					StatementDataGridView.Rows[i].Cells[3].Style.BackColor = Color.LightPink;
					StatementDataGridView.Rows[i].Cells[4].Style.BackColor = Color.LightPink;
					if(account.Currency.Name != Statements[i].Currency.Name)
					{
						sum -= Statements[i].TotalAmount /fm.GetRate(account.Currency, Statements[i].Currency);
					}
					else
					{
						sum -= Statements[i].TotalAmount;
					}
				}

			}
			decimal endBalance = 0;
			new ExceptionCatcher().Execute(() =>{endBalance= fm.GetBalanceAfterPeriod(DateTo, account); });

			label2.Text += String.Format("{0:0.00}",endBalance) + " " + account.Currency;
			label1.Text += String.Format("{0:0.00}",endBalance - sum) + " " + account.Currency;
		}

		

		public FormManager fm { get; set; }
		


		public DialogResult Show(FormManager fm)
		{
			this.fm = fm;
			return ShowDialog();
		}
	}
}
